/*
 * jlb
 */
package com.jlb.my.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.baidu.fsg.uid.UidGenerator;
import com.jlb.core.base.BaseDAO;
import com.jlb.core.base.BaseSVImpl;
import com.jlb.core.entity.Page;
import com.jlb.core.exceptions.BaseException;
import com.jlb.core.exceptions.MyGroupException;
import com.jlb.my.dao.MyGroupDAO;
import com.jlb.my.entity.MyGroup;
import com.jlb.my.entity.MyGroupState;
import com.jlb.setting.dao.SettingDAO;
import com.jlb.setting.entity.Setting;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("myGroupSV")
@Slf4j
public class MyGroupSVImpl extends BaseSVImpl<MyGroup, Long> implements MyGroupSV {

    @Autowired
    private MyGroupDAO myGroupDAO;

    @Autowired
    private SettingDAO settingDAO;
    @Resource
    private UidGenerator uidGenerator;

    @Override
    protected BaseDAO getBaseDAO() {
        return myGroupDAO;
    }

    /**
     * 保存account对象
     *
     * @param entity 实体
     * @throws BaseException
     */
    @Override
    public void save(MyGroup entity) throws BaseException {
        super.save(entity);
    }

    /**
     * 加载一个对象MyGroup
     *
     * @param id * @param userCode 用户编码
     * @return MyGroup
     */
    @Override
    public MyGroup load(java.lang.Long id, java.lang.String userCode) {
        if (id == null && userCode == null) {
            throw new MyGroupException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }

        Map<String, Object> param = new HashMap<>();
        param.put("id", id);
        param.put("userCode", userCode);
        return myGroupDAO.load(param);
    }

    /**
     * 加载一个对象MyGroup 通过id
     *
     * @param id
     * @return MyGroup
     */
    @Override
    public MyGroup loadById(java.lang.Long id) {
        if (id == null) {
            throw new MyGroupException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        return myGroupDAO.loadById(id);
    }

    /**
     * 加载一个对象MyGroup 通过userCode
     *
     * @param userCode 用户编码
     * @return MyGroup
     */
    @Override
    public MyGroup loadByUserCode(java.lang.String userCode) {
        if (userCode == null) {
            throw new MyGroupException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        return myGroupDAO.loadByUserCode(userCode);
    }


    /**
     * 根据主键id,oldStates 共同更新 MyGroup 的状态到newState状态
     *
     * @param id
     * @param newState  新状态
     * @param oldStates 旧状态集合
     */
    @Override
    public void updateStateById(java.lang.Long id, MyGroupState newState, MyGroupState... oldStates) {
        if (id == null) {
            throw new MyGroupException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        if (newState == null) {
            throw new MyGroupException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        if (oldStates == null) {
            throw new MyGroupException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        myGroupDAO.updateStateById(id, new Date(), newState, oldStates);
    }

    /**
     * 根据主键userCode,oldStates 共同更新 MyGroup 的状态到newState状态
     *
     * @param userCode  用户编码
     * @param newState  新状态
     * @param oldStates 旧状态集合
     */
    @Override
    public void updateStateByUserCode(java.lang.String userCode, MyGroupState newState, MyGroupState... oldStates) {
        if (userCode == null) {
            throw new MyGroupException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        if (newState == null) {
            throw new MyGroupException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        if (oldStates == null) {
            throw new MyGroupException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        myGroupDAO.updateStateByUserCode(userCode, new Date(), newState, oldStates);
    }

    /**
     * 根据主键id 更新 MyGroup 的状态到另一个状态
     *
     * @param id
     * @param state 状态
     */
    @Override
    public void updateById(java.lang.Long id, MyGroupState state) {
        if (id == null) {
            throw new MyGroupException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        myGroupDAO.updateById(id, state, new Date());
    }

    /**
     * 根据主键userCode 更新 MyGroup 的状态到另一个状态
     *
     * @param userCode 用户编码
     * @param state    状态
     */
    @Override
    public void updateByUserCode(java.lang.String userCode, MyGroupState state) {
        if (userCode == null) {
            throw new MyGroupException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        myGroupDAO.updateByUserCode(userCode, state, new Date());
    }


    /**
     * 删除对象MyGroup
     *
     * @param id * @param userCode 用户编码
     * @return MyGroup
     */
    @Override
    public void delete(java.lang.Long id, java.lang.String userCode) {
        if (id == null && userCode == null) {
            throw new MyGroupException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        Map<String, Object> param = new HashMap<>();
        param.put("id", id);
        param.put("userCode", userCode);
        myGroupDAO.delete(param);
    }


    /**
     * 查询MyGroup分页
     *
     * @param myGroup 对象
     * @param offset  查询开始行
     * @param limit   查询行数
     * @return List<MyGroup>
     */
    @Override
    public List<MyGroup> list(MyGroup myGroup, int offset, int limit) {
        if (offset < 0) {
            offset = 0;
        }

        if (limit < 0) {
            limit = Page.limit;
        }

        Map<String, Object> map = null;
        if (myGroup != null) {
            map = JSON.parseObject(JSON.toJSONString(myGroup, SerializerFeature.WriteDateUseDateFormat));
        } else {
            map = new HashMap<>();
        }
        return myGroupDAO.list(map, new RowBounds(offset, limit));
    }

    @Override
    public int count(MyGroup myGroup) {
        Map<String, Object> map = null;
        if (myGroup != null) {
            map = JSON.parseObject(JSON.toJSONString(myGroup, SerializerFeature.WriteDateUseDateFormat));
        } else {
            map = new HashMap<>();
        }
        return myGroupDAO.count(map);
    }

    /**
     * 查询MyGroup分页
     *
     * @param id     * @param userCode  用户编码
     * @param offset 查询开始行
     * @param limit  查询行数
     * @return List<MyGroup>
     */
    @Override
    public List<MyGroup> list(java.lang.Long id, java.lang.String userCode, int offset, int limit) {
        if (offset < 0) {
            offset = 0;
        }

        if (limit < 0) {
            limit = Page.limit;
        }
        Map<String, Object> param = new HashMap<>();
        param.put("id", id);
        param.put("userCode", userCode);
        return myGroupDAO.list(param, new RowBounds(offset, limit));
    }

    @Override
    public int count(java.lang.Long id, java.lang.String userCode) {
        Map<String, Object> param = new HashMap<>();
        param.put("id", id);
        param.put("userCode", userCode);
        return myGroupDAO.count(param);
    }

    /**
     * 查询某个用户的团队人数
     *
     * @param userCode 用户编码
     * @return int
     */
    @Override
    public int countGroup(String userCode) {
        int layers = Integer.parseInt(settingDAO.loadByK(Setting.Key.Layers.name()).getV());
        return myGroupDAO.countGroup(userCode, layers);
    }

}
